﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNet.Model;
using System.Data;
using NBear.Mapping;
using DotNet.Utilities;
//using KingRoad.EPM.InvestmeEntity;
using DotNet.Service;
using DotNet.Model.MyEntity;
using KingRoad.EPM.Web.ViewModels;

namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    /// <summary>
    /// 模块（菜单）管理
    /// 功能描述：模块（菜单）的增删改查、移动
    /// 模块负责人：苏千军
    /// 完成日期：2011.9.23
    /// </summary>
    public class ModuleController : Controller
    {
        //
        // GET: /Permission/Module/

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获得模块列表
        /// </summary>
        /// <returns>json数据</returns>
        public JsonResult GetModuleMenus()
        {
            //修改为获取当前登录人具有查看权限的模块 WS 2011.11.2
            List<BaseModuleEntity> list = ServiceManager.Instance.ModuleService.GetCurrentModules() as List<BaseModuleEntity>;
            return Json(list);
        }

        /// <summary>
        /// 左侧菜单树
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult LeftMenu()
        {
            List<BaseModuleEntity> list = ServiceManager.Instance.ModuleService.GetCurrentModules() as List<BaseModuleEntity>;
            if (list != null)
            {
                list.ForEach(x =>
                {
                    if (x.Code == BaseSystemInfo.RootMenuCode)
                    {
                        ViewBag.PID = x.Id.Value.ToString();
                    }
                });
            }
            return View(list);
        }
        //详情
        // GET: /Permission/Module/Details/5
        public ActionResult Details(int id)
        {
            Base_Module module = ServiceManager.Instance.ModuleService.GetEntity(id.ToString());
            ViewBag.ParentName = module.ParentId == null || module.ParentId == 0 ? "" : ServiceManager.Instance.ModuleService.GetEntity(module.ParentId.ToString()).FullName;
            return View(module);
        }

        //新增页面的加载
        // GET: /Permission/Module/Create
        public ActionResult Create(int id)
        {
            ViewBag.ID = id;
            ViewBag.Flag = "Create";
            ViewBag.ParentName = ServiceManager.Instance.ModuleService.GetEntity(id.ToString()).FullName;
            return View();
        }

        //新增页面的提交处理
        // POST: /Permission/Module/Create
        [HttpPost]
        public ActionResult Creating(Base_Module module)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            if (ModelState.IsValid)//实体验证
            {
                string id = ServiceManager.Instance.ModuleService.AddModule(module, out statuCode, out statuMessage);
                if(!string.IsNullOrEmpty(id))
                    module.Id = Convert.ToInt32(id);
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode == "OKAdd" ? "200" : "300",
                callbackType = "Create",
                data = module
            });
        }

        //编辑页面的加载
        // GET: /Permission/Module/Edit/5
        public ActionResult Edit(int id)
        {
            Base_Module module = ServiceManager.Instance.ModuleService.GetEntity(id.ToString());
            ViewBag.Flag = "Edit";
            ViewBag.ParentName = module.ParentId == null || module.ParentId == 0 ? "" : ServiceManager.Instance.ModuleService.GetEntity(module.ParentId.ToString()).FullName;
            return View("Create", module);
        }

        //编辑页面的提交加载
        // POST: /Permission/Module/Edit/5
        [HttpPost]
        public ActionResult Editing(Base_Module module)
        {
            string statuCode = string.Empty;
            string statuMessage = string.Empty;
            if (ModelState.IsValid)//实体验证
            {
                ServiceManager.Instance.ModuleService.UpdateModule(module, out statuCode, out statuMessage);
            }
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode == "OKUpdate" ? "200" : "300",
                callbackType = "Edit",
                data = module
            });
        }

        //删除操作
        // GET: /Permission/Role/Delete/5
        public ActionResult Delete(int id)
        {
            string statuCode;
            string statuMessage;
            ServiceManager.Instance.ModuleService.DeleteModule(id.ToString(), out statuCode, out statuMessage);
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "closeCurrent"
            });
        }

        /// <summary>
        /// 弹出选择模块树的功能
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectTree()
        {
            return View();
        }
        /// <summary>
        /// 节点拖拽操作
        /// </summary>
        /// <param name="id">拖拽的节点id</param>
        /// <param name="pid">目的节点id</param>
        /// <returns></returns>
        public ActionResult Move(int id, int pid)
        {
            string statuCode;
            string statuMessage;
            ServiceManager.Instance.ModuleService.BatchModuleTO(id.ToString(), pid.ToString(), out statuCode, out statuMessage);
            Base_Module module = ServiceManager.Instance.ModuleService.GetEntity(id.ToString());
            return Json(new CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode,
                callbackType = "closeCurrent",
                data = module
            });
        }
        /// <summary>
        /// 移动的加载页面
        /// </summary>
        /// <param name="id">要移动的节点id</param>
        /// <returns></returns>
        public ActionResult MoveNode(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        /// <summary>
        /// 当前用户可操作的所有模块菜单
        /// CreatUser: ThinkWang
        /// CreatTime: 2012-9-25 16:08:42
        /// </summary>
        /// <remarks>首页快捷操作选择模块时使用</remarks>
        /// <returns></returns>
        public ActionResult ModuleList(int index)
        {
            //获取权限系统中当前登录人所能访问的所有菜单
            List<BaseModuleEntity> list = ServiceManager.Instance.ModuleService.GetCurrentModules() as List<BaseModuleEntity>;
            //如果当前登录人是管理员，则进行模块过滤，只显示权限管理、日志管理 WS 2011.11.8
            List<BaseModuleEntity> adminList = new List<BaseModuleEntity>();
            //if (UserService.CurrUserInfo.IsAdministrator)
            //{
            //    string tags = "DotNet";
            //    int permissionID = 0, logID = 0;
            //    //1找到含有这三个标记的模块
            //    list.ForEach(x =>
            //    {
            //        if (!String.IsNullOrEmpty(x.Code))
            //        {
            //            if (tags.Contains(x.Code))
            //                adminList.Add(x);
            //            if (x.Code == "PERMISSION")
            //                permissionID = x.Id.Value;
            //            if (x.Code == "LOG")
            //                logID = x.Id.Value;
            //        }
            //    });
            //    //2找到所有父节点是后两个tag的所有模块
            //    list.ForEach(x =>
            //    {
            //        if (x.ParentId.HasValue)
            //            if (x.ParentId.Value == permissionID || x.ParentId.Value == logID)
            //                adminList.Add(x);
            //    });
            //    list = adminList;
            //}
            if (list != null)
            {
                
                list.ForEach(x =>
                {
                    if (x.Code == "DotNet")
                    {
                        ViewBag.PID = x.Id.Value.ToString();
                    }
                });
            }
            ViewBag.pIndex = index;
            return View(list);
        }
    }
}
